package com.example.jacky.android_easeui.utils;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.example.jacky.android_easeui.DemoApplication;
import com.jaeger.library.StatusBarUtil;

/**
 * Created by xqc on 2016/9/27.
 * Developer App
 */
public class MyStatusBarUtils {
    private static final String TAG = "MyStatusBarUtils";

    /**
     * 针对根布局是 CoordinatorLayout, 使状态栏半透明
     * <p/>
     * 适用于图片作为背景的界面,此时需要图片填充到状态栏
     *
     * @param activity       需要设置的activity
     * @param statusBarAlpha 状态栏透明度
     */
    public static void setTranslucentForCoordinatorLayout(Activity activity, int statusBarAlpha, boolean isNavTrans) {
        StatusBarUtil.setTranslucentForCoordinatorLayout(activity, statusBarAlpha);
        setNavTrans(activity, isNavTrans);
    }

    public static void setTranslucentStatus(Activity activity, boolean isNavTrans) {
        StatusBarUtil.setTranslucent(activity, 1);
        setNavTrans(activity, isNavTrans);
    }

    public static void setTransparentForImageViewInFragment(Activity activity, View needOffsetView, boolean isNavTrans) {
        StatusBarUtil.setTransparentForImageViewInFragment(activity, needOffsetView);
        setNavTrans(activity, isNavTrans);
    }

    private static void setNavTrans(Activity activity, boolean isNavTrans) {
        if (!isNavTrans && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    public static int getStatusBarHeight() {
        // 获得状态栏高度
        int resourceId = DemoApplication.getContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
        return DemoApplication.getContext().getResources().getDimensionPixelSize(resourceId);
    }

    public static int getNavigationBarHeight() {
        int navigationBarHeight = 0;
        int id = DemoApplication.getContext().getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if (id > 0) {
            navigationBarHeight = DemoApplication.getContext().getResources().getDimensionPixelSize(id);
        }
        return navigationBarHeight;
    }

    public static void setMarginStatusBar(View view) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(0, getStatusBarHeight(), 0, 0);
    }

    //填充回调
//    ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {
//        @Override
//        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
//            final int statusBar = insets.getSystemWindowInsetTop();
//            final int navigationBar = insets.getSystemWindowInsetBottom();
//            return insets;
//        }
//    });
}
